package com.gxa.service.Impl;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/06 10:55
 * Description:
 * Version: V1.0
 */

import com.gxa.common.R;
import com.gxa.mapper.zhy.RoleAuthorizationMapper;
import com.gxa.pojo.Role;
import com.gxa.pojo.Role_Permission;
import com.gxa.pojo.zhy.EditPermission;
import com.gxa.pojo.zhy.JsonResult;
import com.gxa.pojo.zhy.Paging;
import com.gxa.service.RoleAuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色权限对应的Service
 */
@Service
public class RoleAuthorizationServiceImpl implements RoleAuthorizationService {

    @Autowired
    private RoleAuthorizationMapper roleAuthorizationMapper;
    /**
     * 根据当前页码数和每页显示的数量查询所有的角色以及相关信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R queryAllRoles(Integer currentPage, Integer pageSize) {
         JsonResult jsonResult=new JsonResult();
         List<Object> resultList=new ArrayList<>();
        R r=new R();
        Integer totalSize=roleAuthorizationMapper.querytotalSize();
        Integer param1=(currentPage-1)*pageSize;
        Integer param2=pageSize;
        List<Object> objectlist=roleAuthorizationMapper.queryAllRoles(param1,param2);
        if(objectlist!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalSize);
            resultList.add(objectlist);
            resultList.add(jsonResult);
            r.setData(resultList);
        }else if(objectlist==null) {
            r.setCode(504);
            r.setMsg("无数据");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }

        return r;
    }
    /**
     * 根据角色id(role_id)删除角色
     * @param role_id
     * @return
     */
    @Override
    public R deleteRoleByRole_id(Integer role_id) {
        R r=new R();
        int result=roleAuthorizationMapper.deleteRoleByRole_id(role_id);
        if(result>=0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
// else {
//            r.setCode(500);
//            r.setMsg("后台服务错误");
//        }
        return r;
    }
    /**
     * 添加角色
     * @param role_name
     * @param role_describe
     * @return
     */
    @Override
    public R addRole(String role_name, String role_describe) {
        R r=new R();
        /**
         * 判断该角色是否已经存在（添加角色时）
         */
        int result1 =roleAuthorizationMapper.selectRole_nameExists(role_name);
        if(result1>0){
            r.setCode(501);
            r.setMsg("添加失败，该角色已经存在");
        }else{
            /**
             * 进行添加角色
             */
        int result2=roleAuthorizationMapper.addRole(role_name,role_describe);
        if(result2>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }else {
            r.setCode(500);
            r.setMsg("添加失败后台服务错误");
        }
        }
        return r;
    }
    /**
     * 根据角色id请求显示其对应的所有的权限
     * @param role_id
     * @return
     */
    @Override
    public R queryAllRolesByRole_id(Integer role_id) {
        R r=new R();
        List<Role_Permission> role_permissionList=roleAuthorizationMapper.queryAllRolesByRole_id(role_id);
        if(role_permissionList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(role_permissionList);
        }else if(role_permissionList==null){
            r.setCode(504);
            r.setMsg("该角色无权限");
        }else{
            r.setCode(500);
            r.setMsg("服务器错误");
        }
         return r;
    }
    /**
     * 修改角色信息（名字，描述）
     * @param role
     * @return
     */
    @Override
    public R updateRolesInformationbyRoleId(Role role) {
        R r=new R();
       int result= roleAuthorizationMapper.updateRolesInformationbyRoleId(role);
       if(result>0){
           r.setCode(200);
           r.setMsg("修改成功");
       }else {
           r.setCode(504);
           r.setMsg("修改失败");
       }
        return r;
    }
    /**
     * 根据角色id修改角色对应的权力信息(改中间表)
     * @param
     * @return
     */
    @Override
    public R updatePermissionByRoleId(EditPermission editPermission) {
        R r=new R();
        int totalAdd=0;

//        判断是否提交空的权限值
       if(editPermission.getPermissionList().size()==0){
           r.setCode(504);
           r.setMsg("不可提交空的权限");
       }else {
           /**
            * 先根据角色id删除对应的权限
            */
           int resultDel=roleAuthorizationMapper.deletePermissionByRoleId(editPermission.getRole_id());
           if(resultDel>=0){
               /**
                * 先根据角色id添加对应的权限
                */
               for (int i=0;i<editPermission.getPermissionList().size();i++){
                   int resultAdd=roleAuthorizationMapper.addPermissionByRoleId(editPermission.getRole_id(),editPermission.getPermissionList().get(i));
                   //判断添加了多少次
                   if(resultAdd>0) {
                       totalAdd++;
                   }
               }
               if(totalAdd>=0){
                   r.setCode(200);
                   r.setMsg("修改成功");
               }
           }else {
               r.setCode(500);
               r.setMsg("服务器错误，删除失败");
           }

       }
        return r;
    }


    /**
     * 模糊搜索显示所有的角色
     * @param paging
     * @return
     */
    @Override
//    public R querySearchRolesbyKeyword(Paging paging) {
    public R querySearchRolesbyKeyword(Integer currentPage,Integer pageSize,String message) {
        JsonResult jsonResult=new JsonResult();
        List<Object> resultList=new ArrayList<>();
        R r=new R();
        Integer totalSize=roleAuthorizationMapper.querytotalSizes(message);
//        Integer totalSize=roleAuthorizationMapper.querytotalSizes(paging.getMessage());
//        Integer param1=(paging.getCurrentPage()-1)*paging.getPageSize();
        Integer param1=(currentPage-1)*pageSize;
//        Integer param2=paging.getPageSize();
        Integer param2=pageSize;
//        List<Role> roleList=roleAuthorizationMapper.queryAllRolesBYMessage(param1,param2,paging.getMessage());
        List<Role> roleList=roleAuthorizationMapper.queryAllRolesBYMessage(param1,param2,message);
        if(roleList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalSize);
            resultList.add(roleList);
            resultList.add(jsonResult);
            r.setData(resultList);
        }else if(roleList==null) {
            r.setCode(504);
            r.setMsg("无数据");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }

        return r;
    }


}
